<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
            router
        >
            <el-menu-item index="dashboard" key="dashboard">
                <i class="el - icon - lx - home"></i>
                <span slot="title">系统首页</span>
            </el-menu-item>
            <template v-for="menu in menus">
                <el-submenu :index="menu.link" :key="menu.link">
                    <template slot="title">
                        <i :class="menu.icon"></i>
                        <span slot="title">{{ menu.name }}</span>
                    </template>

                    <template v-for="subMenu in menu.subMenu">
                        <el-menu-item :index="subMenu.link" :key="subMenu.link">{{ subMenu.name }} </el-menu-item>
                    </template>
                </el-submenu>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
export default {
    data() {
        return {
            collapse: false,
            menus: []
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', (msg) => {
            // this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });

        this.$axios.get('/admin/allMenus?account='+localStorage.getItem("ms_username")).then((r) => {
            this.menus = r.data.data;
            console.log(this.menus);

            // for(const m of r.data.data){
            //     console.log(menu);
            //     let menus = [];

            //     menus.push
            // }
        });
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
    width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}

.sidebar > ul {
    height: 100%;
}
</style>
